$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
layui.use('table', function() {
			var table = layui.table;
			//监听表格复选框选择
			table.on('checkbox(demo)', function(obj) {
				console.log(obj)
			});
			//监听工具条
			table.on('tool(demo)', function(obj) {
				var data = obj.data;
				if (obj.event === 'del') {
					layer.confirm('确定删除该用户吗？', function(index) {
						layer.close(index);
						var id = data.usersId;
						var url = "../../userAction/delete.action";
						var datas = {
							'usersId' : id
						};
						$.ajax({
							asyn : true,
							type : "POST",
							url : url,
							data : datas,
							success : function(msg) {
								if (msg.state == 1) {
									layer.msg(msg.msg);
									table.reload('idTest');
								} else {
									layer.msg(msg.msg);
								}
							},
							error : function(fh) {
								layer.msg(msg.msg);
							}
						},"json");
					});
				} else if (obj.event === 'edit') {
					$('#updateReload').css('display','block');
					$("#usersId").val(data.usersId);
					$("#fsId").val(data.fsId);
					$("#usersName").val(data.usersName);
					$("#usersPwd").val(data.usersPwd);
					$("#usersPhone").val(data.usersPhone);
					$("#usersCarid").val(data.usersCarid);
				    $("#usersIsva").find("option[value="+data.usersIsva+"]").attr("selected","selected");
					$.ajax({
						asyn : true,
						type : "POST",
						url : "../../userAction/findRoleByStaff.action",
						success : function(msg) {
							var htmlStr = '';
							for(var i=0;i<msg.length;i++){
								htmlStr+='<input type="checkbox" name="role" value="'+msg[i].roleId+'" title="'+msg[i].roleName+'" >';
							}
							$("#roles").html(htmlStr);
							$.post("../../userAction/findRoleByManId.action",{"usersId":data.usersId},function(usRole){
								for(var i=0;i<usRole.length;i++){
									$("input[name='role']").each(function(){
			                            if($(this).val()==usRole[i].roleId){    
			                                $(this).prop('checked',true);    
			                            }
			                        });
								}
								layui.form.render();
							},"json");
							$("#update").click(function(){
								var str = "";
		                        $("input[name='role']").each(function(){    
		                            if($(this).is(":checked")){    
		                                str += $(this).val()+"-";    
		                            }    
		                        });
		                        $("#roleIds").val(str);
								$("#updateMess").submit();
							});
							layui.form.render();
						},
						error : function(fh) {
						}
					});
				}
			});
			var $ = layui.$, active = {
				reload : function() {
					//执行重载
					table.reload('idTest', {
						where : {
							'usersName' : $("#name").val(),
							'usersPhone' : $("#phone").val()
						},
						page : {
							curr : 1
						}
					});
				}
			};
			$('#closeAdd').on('click', function() {
				$('#addReload').css('display', 'none');
			});
			$('#closeUpdate').on('click', function() {
				
				$('#updateReload').css('display', 'none');
			});
			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
		});
		function add() {
			$('#addReload').css('display', 'block');
			$.ajax({
				asyn : true,
				type : "POST",
				url : "../../userAction/findRoleByStaff.action",
				success : function(msg) {
					var htmlStr = '';
						for(var i=0;i<msg.length;i++){
							htmlStr+='<input type="checkbox" name="role" value="'+msg[i].roleId+'" title="'+msg[i].roleName+'" >';
						}
						$("#Addroles").html(htmlStr);
					$("#add").click(function(){
						var str = "";    
                        $("input[name='role']").each(function(){    
                            if($(this).is(":checked")){    
                                str += $(this).val()+"-";    
                            }    
                        });
                        $("#addRoleIds").val(str);
						$("#addMess").submit();
					});
					layui.form.render();
				},
				error : function(fh) {
				}
			});
		}
var curPage = 1; //当前页数
var maxPage = 0; //最大页数
$(function(){
	pages(null);
	$("#query").click(function() {
		$("#page").val(1);
	    $("#limit").val(10);
	    var data = $("#queryForm").serializeObject(); //将表单序列化
        loadData(data);
    })
    
})
/**
分页
*/
function pages(data){
	$("#pages").html("");
	var url = "../../userAction/findPage.action";
	$.post(url,data,function(msg){
		var htmlStr="";
		htmlStr+="<li><a href='javascript:pageReload("+ (curPage - 1) + ")'>&laquo;</a></li>";
		maxPage = msg.totalPage;
		for(var i=1;i<=maxPage;i++){
			htmlStr+="<li class='pageColor' name="+i+"><a href='javascript:pageReload(\"" + i + "\")'>"+i+"</a></li>";
		}
		htmlStr+="<li><a href='javascript:pageReload("+ (curPage + 1) + ")'>&raquo;</a></li>";
		$("#pages").html(htmlStr);
		pageColor(curPage);
	},"json");
}
function pageColor(page){
	$('.active').each(function(){
		$(this).attr("class","pageColor");
	});
	$('.pageColor').each(function(){
		if(page==$(this).attr("name")){
			$(this).attr("class","active");
		}else{
			$(this).attr("class","pageColor");
		}
	});
}
//分页的方法
function pageReload(pages) {
    if (pages == 0) {
        alert("已经第一页了");
        return;
    } else if (pages > maxPage) {
        alert("已经最后一页了");
        return;
    }
    pageColor(pages);
    curPage = pages;
    $(".pagination").children(":first").html('<a href="javascript:pageReload(' + (curPage - 1) + ')">&laquo;</a>')
    $(".pagination").children(":last").html('<a href="javascript:pageReload(' + (curPage + 1) + ')">&raquo;</a>')
    $("#page").val(pages);
    $("#limit").val(10);
    var data = $("#queryForm").serializeObject(); //将表单序列化
    loadData(data);
}

function loadData(data) {
	var table = layui.table;
	table.reload('idTest', {
		where : data
	});
	pages(data);
}
function hoverOpenImg(){
    var img_show = null; // tips提示
    $('td img').hover(function(){
        //alert($(this).attr('src'));
        var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:130px;' />";
        img_show = layer.tips(img, this,{
            tips:[2, 'rgba(41,41,41,.5)']
            ,area: ['160px']
        });
    },function(){
        layer.close(img_show);
    });
    $('td img').attr('style','max-width:70px');
}